class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution(object):
    def reverseBetween(self, head, left, right):
        """
        :type head: ListNode
        :type left: int
        :type right: int
        :rtype: ListNode
        """
        mute_node = ListNode()
        mute_node.next = head
        head = mute_node
        index = 0
        node = head
        if left == right:
            return head.next
        else:
            while index < left - 1:
                node = node.next
                index += 1
            last_node = node
            node = node.next
            fist_node = node
            prev_node = node
            next_node = node.next
            index += 1
            while index < right:
                index += 1
                node = next_node
                next_node = next_node.next
                node.next = prev_node
                prev_node = node
            last_node.next = prev_node
            fist_node.next = next_node
            return head.next



